using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using System.IO;
using UMC.Web;
using UMC.Web.UI;
using System.Linq;

namespace UMC.Wiki.Activities
{



    class SubjectSinleUIActivity : WebActivity
    {
        void Search(UISection ui, IEnumerable<Subject> subjects, String model, String cmd, Project project, ProjectItem item, Portfolio portfolio, bool isApp, Uri uri)
        {

            var items = ui;
            var webr = UMC.Web.WebResource.Instance();


            foreach (var sub in subjects)
            {
                if (sub.Status == -2)
                {
                    continue;
                }

                int[] imageIndexs = Utility.BitIndex(sub.ImageIndexs ?? 0);
                var imgs = new List<String>();

                switch (imageIndexs.Length)
                {
                    case 0:
                        break;
                    case 2:
                    case 1:
                        if (isApp)
                        {
                            imgs.Add(webr.ImageResolve(uri, sub.Id, imageIndexs[0], (sub.IsPicture ?? false) ? "-1" : "-3", "_ts=" + sub.ImageIndexs));

                        }
                        else
                        {
                            imgs.Add(webr.ImageResolve(uri, sub.Id, imageIndexs[0], "-3", "_ts=" + sub.ImageIndexs));

                        }

                        break;
                    default:
                        for (var i = 0; i < 3; i++)
                        {
                            imgs.Add(webr.ImageResolve(uri, sub.Id, imageIndexs[i], "-3", "_ts=" + sub.ImageIndexs));
                        }
                        break;
                }
                var click = new Web.UIClick(model, cmd, sub.Id.ToString());
                var data = new UMC.Web.WebMeta().Put("title", sub.Title).Put("reply", (sub.Reply ?? 0).ToString()).Put("look", (sub.Look ?? 0).ToString());

                data.Put("pname", item.Caption);

                data.Put("time", Utility.GetDate(sub.ReleaseTime));
                data.Put("desc", sub.Description);
                UICMS cell;
                switch (imgs.Count)
                {
                    case 0:
                        cell = new UICMS(click, data);
                        break;
                    default:
                        cell = (sub.IsPicture ?? false) ? new UICMS(click, data, imgs[0], isApp) : (imgs.Count > 2 ? new UICMS(click, data, imgs[0], imgs[1], imgs[2]) : new UICMS(click, data, imgs[0]));
                        break;
                }
                cell.Formats.Put("left", "{pname} {iname} {time}");
                cell.Style.Name("licon", new UIStyle().Size(12).Font("wdk")).Name("ricon", new UIStyle().Size(12).Font("wdk"));
                cell.Style.Name("pname").Color(0x777);
                cell.Style.Name("iname").Color(0x777);


                cell.Formats.Put("right", "\uF06E{1:licon} {look}   \uF0E6{1:ricon} {reply}");
                items.Add(cell);
            }

        }

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            // var search = new Subject();
            var webr = UMC.Web.WebResource.Instance();
            Project project = null;
            var key = this.AsyncDialog("Project", "umc");


            var pid = UMC.Data.Utility.UInt64Parse(key);

            if (pid.HasValue == false)
            {
                project = DataFactory.Instance().Project(key);
            }
            else
            {
                project = DataFactory.Instance().Project(pid.Value);
            }

            if (project == null)
            {
                this.Prompt("未有标识项目");
            }

            var items = DataFactory.Instance().ProjectItem(new Project { Id = project.Id });

            var ui = UISection.Create();
            var isFrist = true;
            foreach (var item in items)
            {
                if ((item.Hide ?? false) == false)
                {

                    var ps = DataFactory.Instance().Portfolio(item).OrderBy(r => r.Sequence ?? 0).ToArray();

                    var subs = DataFactory.Instance().Subject(ps);

                    foreach (var p in ps)
                    {
                        if (isFrist)
                        {
                            isFrist = false;
                            var sus = subs.Where(r => r.portfolio_id == p.Id).OrderBy(r => r.Seq ?? 0);
                            Search(ui, sus, request.Model, "UIData", project, item, p, request.IsApp, request.Url);
                        }
                        else
                        {
                            var sus = subs.Where(r => r.portfolio_id == p.Id).OrderBy(r => r.Seq ?? 0);
                            var ui2 = ui.NewSection();
                            ui2.Header.Put("text", p.Caption);
                            Search(ui2, sus, request.Model, "UIData", project, item, p, request.IsApp, request.Url);
                        }
                    }

                }
            }

            ui.SendTo(this.Context);
        }

    }
}